﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IP2Client
{
    public partial class PatientHistorySelect : Form
    {
        private List<String> patientList = new List<String>();
        private BindingList<string> bindingList;

        /// <summary>
        /// constructor for patientHistorySelect
        /// </summary>
        public PatientHistorySelect()
        {
            InitializeComponent();

            getAllPatientsFromServer();

            handleTcpClientUpdates();                     
        }

        /// <summary>
        /// Creates a new patienHistoryForm if a valid patient is selected.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void selectPatientButton_Click(object sender, EventArgs e)
        {
            if (patientsComboBox.SelectedIndex == -1)
            {
                MessageBox.Show("Select a patient before clicking select!", "Error"); return;
            }

            if (patientsComboBox.SelectedIndex != -1)
            {
                this.Hide();
                this.Dispose();

                Form patientHistory = new PatientHistoryFile(patientsComboBox.SelectedItem.ToString());           
                patientHistory.ShowDialog();                
            }
        }

        /// <summary>
        /// Asks the server to send all patients
        /// </summary>
        private void getAllPatientsFromServer()
        {
            ConnectionHandler.SendObject("!sndAllPatients");
        }

        /// <summary>
        /// 
        /// </summary>
        public void handleTcpClientUpdates()
        {
            object rcvdObject = null;

            if (ConnectionHandler.HandledClient == null)
            {
                return;
            }

            while (rcvdObject == null)
            {
                if (ConnectionHandler.HandledClient.Available > 0)
                {           
                    rcvdObject = ConnectionHandler.ReadObject();

                    patientList = rcvdObject as List<String>;

                    if (patientList != null && patientList.Count != 0)
                    {
                        bindingList = new BindingList<string>(patientList);
                        patientsComboBox.DataSource = bindingList;
                    }
                }

                System.Threading.Thread.Sleep(100);
            }
        }

        /// <summary>
        /// The application is exitted.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
